﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MagicKey.Components.Database;
using MagicKey.Factory;
using MagicKey.Interfaces;

namespace MagicKey.UserInterface
{
    /// <summary>
    /// Interaktionslogik für PasswordViewer.xaml
    /// </summary>
    public partial class PasswordViewer : Window
    {
        readonly IAccessManager _accessManager = MagicKeyFactory.GetInstance().GetMagicKeyInterface().GetAccessManager();
        readonly IUserManager _userManager = MagicKeyFactory.GetInstance().GetMagicKeyInterface().GetUserManager();
        readonly IFileAccess _fileAccess = MagicKeyFactory.GetInstance().GetMagicKeyInterface().GetFileAccess();

        public PasswordViewer(Fileviewer fileviewer)
        {
            InitializeComponent();
            this.Owner = fileviewer;
        }

        private void Valided_Click(object sender, RoutedEventArgs e)
        {
            // get pass != string.empty
            if (this.PasswordTextBox.Password.Equals(string.Empty)) return;

            //
            var hashedPassword = _accessManager.HashPassword(this.PasswordTextBox.Password);

            //
            var user = ((Fileviewer)this.Owner).MasterUser;

            //
            var salt = _userManager.GetSalt(user.Name.ToLower());

            //
            //var symetricKey = _userManager.GetSymetricKey(user.Name.ToLower());

            //
            string path = ((Fileviewer)this.Owner).SelectedFile;

            // Hash a hashed password with salt
            hashedPassword = _accessManager.HashHashedPasswordAndSalt(hashedPassword, salt);

            // Load saved password
            var savedPassword = _userManager.GetHashedPassword(user.Name.ToLower());

            //check if given password is correct
            if (savedPassword != null && hashedPassword.Equals(savedPassword))
            {
                if (!_accessManager.HasAuthorization(user.Id, path)) return;

                //Load encrypted file on local dekstop
                _fileAccess.LoadFile(path);

                //compute a path on a local dekstop
                var fullFilename = "C:/magic_key/" + path.Substring(path.LastIndexOf('/') + 1);

                //Get encrypted private key
                var privateKeyEncrypted = _userManager.GetPrivateKeyEncrypted(user.Name.ToLower());

                //decrypt a private key
                var privateKeyDecrypted = _accessManager.DecryptStringSym(privateKeyEncrypted, this.PasswordTextBox.Password);

                // Destroy a password
                this.PasswordTextBox.Password = null;

                ////decrypt a file asymetric with private key
                //var decryptedFileASym = _accessManager.DecryptFileAsym(privateKeyDecrypted, fullFilename);

                var fileOwner = _userManager.GetOwnerName(path);

                //check if a logged user is a owner
                var isOwner = fileOwner == user.Name;

                //Get a encrypted symetric key of logged user 
                string encryptedSymetricKeyOfFile = _accessManager.GetEncryptedSymetricKeyOfFile(path, user.Id, isOwner);

                //decrypt asymetric a symetric key
                var decryptedSymetricKey = _accessManager.DecryptStringAsym(privateKeyDecrypted,
                    encryptedSymetricKeyOfFile);

                privateKeyDecrypted = null;

                //decrypt a file symetric
                var decryptedFileSym = _accessManager.DecryptFileSym(fullFilename, decryptedSymetricKey);

                decryptedSymetricKey = null;
                //
                ((Fileviewer)this.Owner).IsSelectedFileDecrypt = true;

                //open a decrypt file
                Process.Start(decryptedFileSym);

                this.Close();

            }
            else
            {
                MessageBox.Show("Your pawword is wrong", "PasswordViewer",
                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

    }
}
